package com.sphd.Restaurant.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.toolbox.ImageLoader;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sphd.Restaurant.R;
import com.sphd.Restaurant.model.Model;
import com.sphd.Restaurant.utils.JiaMi;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by Hu on 2016/10/24.
 */

public class BaseActivity extends AppCompatActivity {
    public SharedPreferences remember;
    public Context context;
    public View footView;
    private static final String PACKAGE_URL_SCHEME = "package:"; // 方案
    private SweetAlertDialog sweetAlertDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("BaseActivity", "BaseActivity");
        remember = MyApplication.getRemember();
        context = this;
        footView = View.inflate(context, R.layout.foot_view, null);
    }

    public void addFootView(BaseQuickAdapter baseQuickAdapter) {
        footView.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        baseQuickAdapter.removeAllFooterView();
        baseQuickAdapter.addFooterView(footView);
    }

    public void setToolBar(Toolbar toolbar, String titile, boolean b) {
        toolbar.setTitle(titile.trim());
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(b);

    }


    public void setRefresh(SwipeRefreshLayout myRefreshLayout) {

        myRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_light, android.R.color.holo_red_light,
                android.R.color.holo_orange_light, android.R.color.holo_green_light);
    }

    public void setRecycle(RecyclerView mRecy) {
        mRecy.setLayoutManager(new LinearLayoutManager(context));
//        mRecy.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL_LIST));
    }

    public boolean contains(String key) {
        return remember.contains(key);
    }

    public String getStringDate(long t) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        Date d1 = new Date();
        String t1 = format.format(d1);
        return t1;
    }

    public void setRecy(RecyclerView recy) {

        recy.setHasFixedSize(true);
        recy.setLayoutManager(new LinearLayoutManager(context));
//        recy.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }

    public void toast(Object o) {
        if (o instanceof Integer) {
            Toast.makeText(context, (Integer) o, Toast.LENGTH_SHORT).show();
        } else if (o instanceof CharSequence) {
            Toast.makeText(context, (CharSequence) o, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(context, o + "", Toast.LENGTH_SHORT).show();
        }

    }

    public void showDialog() {
        if (sweetAlertDialog == null) {
            sweetAlertDialog = new SweetAlertDialog(context, SweetAlertDialog.PROGRESS_TYPE);
        }

        if (sweetAlertDialog != null) {
            sweetAlertDialog.setTitleText("加载中……");
            if (!sweetAlertDialog.isShowing()) {
                sweetAlertDialog.show();
            }
        }

    }

    public void dissMissDialog() {
        if (sweetAlertDialog != null) {
            if (sweetAlertDialog.isShowing()) {
                sweetAlertDialog.dismiss();
            }
        }
    }

    public boolean isMNC() {
        return Build.VERSION.SDK_INT >= 23;
    }


    public void putString(String key, String val) {
//        remember.edit().putString(jiami(key), jiami(val)).commit();
        remember.edit().putString(key, val).commit();
    }

    public void putBoolean(String key, boolean val) {
//        remember.edit().putBoolean(jiami(key), val).commit();
        remember.edit().putBoolean(key, val).commit();
    }

    public void putInt(String key, int val) {
//        remember.edit().putInt(jiami(key), val).commit();
        remember.edit().putInt(key, val).commit();
    }

    public void putLong(String key, long val) {
//        remember.edit().putLong(jiami(key), val).commit();
        remember.edit().putLong(key, val).commit();
    }

    public String getString(String key) {
//        if (remember.contains(jiami(key))) {
//            String val = remember.getString(jiami(key), null);
//            return jiemi(val);
//        }
        if (remember.contains(key)) {
            String val = remember.getString(key, null);
            return val;
        }
        return null;
    }

    public int getInt(String key) {
        if (remember.contains(key)) {
            return remember.getInt(key, 0);
        }
//        if (remember.contains(jiami(key))) {
//            return remember.getInt(jiami(key), 0);
//        }
        return 0;
    }

    public long getLong(String key) {
//        if (remember.contains(jiami(key))) {
//            return remember.getLong(jiami(key), 0);
//        }
        if (remember.contains(key)) {
            return remember.getLong(key, 0);
        }
        return 0;
    }

    public boolean getBoolean(String key) {
//        if (remember.contains(jiami(key))) {
//            return remember.getBoolean(jiami(key), false);
//        } else {
//            return false;
//        }
        if (remember.contains(key)) {
            return remember.getBoolean(key, false);
        } else {
            return false;
        }
    }

    /**
     * @param val
     * @return 加密
     */
    public String jiami(String val) {
        return JiaMi.setJami(val);
    }

    /**
     * @param val
     * @return 解密
     */
    public String jiemi(String val) {
        return JiaMi.getJiemi(val);
    }

    public void setImage(ImageView image, String url) {
        ImageLoader imageLoader = MyApplication.getImageLoader();
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(image,
                R.mipmap.moren_cg, R.mipmap.moren_cg);
        imageLoader.get(Model.BASEURL + url, listener);
    }

    public void startActivity(Class<?> cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public Bundle crratBundle() {
        Bundle bundle = new Bundle();
        return bundle;
    }

    public void hideFragemts(FragmentTransaction transaction, Fragment fragment) {
        if (fragment != null) {
            transaction.hide(fragment);
        }
    }

    public boolean isNoEmpty(String text) {
        return !TextUtils.isEmpty(text);
    }

    public BigDecimal turn(String val, int i) {
        BigDecimal bd = new BigDecimal(val);
        bd = bd.setScale(i, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入
        return bd;
    }

    public BigDecimal turn(double val, int i) {
        BigDecimal bd = new BigDecimal(val);
        bd = bd.setScale(i, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入
        return bd;
    }

    public BigDecimal turn(float val, int i) {
        BigDecimal bd = new BigDecimal(val);
        bd = bd.setScale(i, BigDecimal.ROUND_HALF_UP);//BigDecimal.ROUND_HALF_UP 是4舍5入
        return bd;
    }

    public String noTrim(BigDecimal s) {
        String ss = s.toString();
        String sss = null;
        if (ss.indexOf(".") > 0) {
            //正则表达
            sss = ss.replaceAll("0+?$", "");//去掉后面无用的零
            sss = ss.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
        }
        return sss;
    }


    public void showAlerDialog(int type, String title) {
        new SweetAlertDialog(
                context, type)
                .setTitleText(title)
                .show();
    }

    public String turnUTF8(String s) {
        try {
            return URLEncoder.encode(s.trim(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void removeKey(String key) {
        remember.edit().remove(key).commit();
    }

    // 启动应用的设置
    public void startAppSettings() {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));
        startActivity(intent);
    }

    public void setGridRecy(RecyclerView recy) {
        recy.setHasFixedSize(true);
        recy.setLayoutManager(new GridLayoutManager(context, 2));
    }

}
